Skip to content

Parser: Improve extract.c to strip <% # comments %> with space#825

Merged
marcoroth merged 1 commit into
mainfrom
parser-improved-comment-extraction
Nov 9, 2025
Merged

Parser: Improve extract.c to strip <% # comments %> with space#825
marcoroth merged 1 commit into
mainfrom
parser-improved-comment-extraction

Conversation

@marcoroth

@marcoroth marcoroth commented Nov 9, 2025

Copy link
Copy Markdown
Owner

Follow up to #824 and #98.

This pull request improves the herb_extract_ruby_to_buffer_with_semicolons function even further, so that it is also be able to deal ERB comments that have a leading space on a single line:

<% if true %><% # Comment here %><%= hello %><% end %>

Before

CleanShot 2025-11-09 at 03 13 24@2x

After

Now, we only have a linter offense telling people to use <%#, but no parser errors anymore.

CleanShot 2025-11-09 at 03 14 16@2x

Resolves #101

@marcoroth marcoroth merged commit a772b70 into main Nov 9, 2025
24 checks passed
@marcoroth marcoroth deleted the parser-improved-comment-extraction branch November 9, 2025 02:21
marcoroth added a commit that referenced this pull request Nov 9, 2025
Follow up to #825.

This pull request improves the engine by not compiling `<% # coments %>
comments as part of the template. Outputting `# coments` could lead to
some cases, where the template could be compiled but couldn't be
rendered, because it would comment out important pieces of the template.

```erb
<% if true %><% # comment %><%= "Hello World" %><% end %>
```

**Before**

`exe/herb compile test.html.erb`
```ruby
__herb = ::Herb::Engine; _buf = ::String.new;
 if true; # comment
 _buf << __herb.h(("Hello World")); end; _buf << '
'.freeze;
_buf.to_s
```


**After**

`exe/herb compile test.html.erb`
```ruby
__herb = ::Herb::Engine; _buf = ::String.new;
 if true; _buf << __herb.h(("Hello World")); end; _buf << '
'.freeze;
_buf.to_s
```


### For reference

`bin/erubi-compile test.html.erb`
```ruby
__erubi = ::Erubi; _buf = ::String.new; if true ; # comment ; _buf << __erubi.h(( "Hello World" )); end ; _buf << '
'.freeze;
_buf.to_s
```

`bin/erubi-render test.html.erb`
```
bin/erubi-render:117:in 'Kernel#eval': (eval at bin/erubi-render:117):2: syntax errors found (SyntaxError)
  1 | __erubi = ::Erubi; _buf = ::String.new; if true ; # comment ; _buf << __erubi.h(( "Hello World" )); end ; _buf << '
> 2 | '.freeze;
    | ^ unterminated string meets end of file
  3 | _buf.to_s
> 4 |
    | ^ expected an `end` to close the conditional clause

    | ^ unexpected end-of-input, assuming it is closing the parent top level context

        from bin/erubi-render:117:in 'ErubiRenderer#render_template'
        from bin/erubi-render:31:in 'ErubiRenderer#run'
        from bin/erubi-render:152:in '<main>'
```

`exe/herb render test.html.erb`
```
Hello World
```
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Support for Inline Ruby Comments for when extracting Ruby code in herb_extract_ruby

1 participant